<template>
    <div>
        <el-breadcrumb separator-class="el-icon-arrow-right">
            <el-breadcrumb-item :to="{ path: '/home' }">
                首页</el-breadcrumb-item
            >
            <el-breadcrumb-item :to="{ path: '/page' }">
                权限管理</el-breadcrumb-item
            >
            <el-breadcrumb-item :to="{ path: '/group' }"
                >权限组管理</el-breadcrumb-item
            >
            <el-breadcrumb-item>添加权限组</el-breadcrumb-item>
        </el-breadcrumb>
        <el-card class="box-card">
            <el-page-header @back="goBack" content="添加权限组">
            </el-page-header>
            <el-form
                :model="addForm"
                :rules="addFormRules"
                ref="addFormRef"
                label-width="120px"
                hide-required-asterisk
            >
                <el-form-item label="父级权限组" prop="parent_id" align="left">
                    <el-tooltip
                        :enterable="false"
                        effect="dark"
                        content="不选择则权限组为根权限组"
                        placement="top"
                    >
                        <select-tree
                            ref="setTree"
                            @subSelectData="setParentId"
                        ></select-tree>
                    </el-tooltip>
                </el-form-item>
                <el-form-item label="权限组名称" prop="name">
                    <el-input
                        v-model="addForm.name"
                        placeholder="请输入权限组名称"
                    >
                    </el-input>
                </el-form-item>
                <el-form-item label="权限组描述" prop="describe">
                    <el-input
                        v-model="addForm.describe"
                        :autosize="{ minRows: 2, maxRows: 4 }"
                        type="textarea"
                        placeholder="请输入权限组描述"
                    />
                </el-form-item>
                <el-form-item label="状态" align="left">
                    <el-switch
                        v-model="addForm.status"
                        :active-value="1"
                        :inactive-value="0"
                    ></el-switch>
                </el-form-item>
                <el-form-item align="left">
                    <el-button type="primary" @click="subAdd"
                        >立即创建</el-button
                    >
                    <el-button @click="resetForm">重置</el-button>
                </el-form-item>
            </el-form>
        </el-card>
    </div>
</template>

<script>
import requestApis from '@/request'
import SelectTree from '@/components/select_tree/SelectTree.vue'
export default {
    components: { SelectTree },
    data () {
        return {
            addForm: {
                parent_id: 0,
                name: '',
                describe: '',
                status: 1
            },
            // 添加表单验证规则对象
            addFormRules: {
                name: [
                    { required: true, message: '请输入权限组名称', trigger: 'blur' },
                    { min: 2, max: 16, message: '权限组名称长度在2~16位之间', trigger: 'blur' }
                ],
                describe: [
                    { required: true, message: '请输入权限组描述', trigger: 'blur' },
                    { min: 2, message: '权限组描述长度必须大于2', trigger: 'blur' }
                ]
            }
        }
    },
    mounted () {
        this.getGroupMenuAll()
    },
    methods: {
        setParentId (parentId) {
            this.addForm.parent_id = parentId
        },
        getGroupMenuAll () {
            requestApis.getGroupMenuAll().then(res => {
                if (res.meta.code === 1) {
                    this.$refs.setTree.setSelectData(res.data)
                } else {
                    this.mg({
                        message: res.meta.msg,
                        type: 'warning'
                    })
                }
            }).catch(error => {
                console.log(error)
            })
        },
        goBack () {
            this.$router.back()
        },
        subAdd () {
            this.$refs.addFormRef.validate(valid => {
                if (!valid) {
                    return false
                }
                requestApis.saveGroupAdd(this.addForm).then(res => {
                    if (res.meta.code === 1) {
                        this.$message({
                            message: res.meta.msg,
                            type: 'success'
                        })
                        this.$router.back()
                    } else {
                        this.$message({
                            message: res.meta.msg,
                            type: 'warning'
                        })
                    }
                }).catch(error => {
                    console.log(error)
                })
            })
        },
        resetForm () {
            this.$refs.addFormRef.resetFields()
            this.addForm.status = 1
        }
    }
}
</script>

<style>
.el-form {
    width: 60%;
    margin-top: 20px;
}
</style>
